#include "Frm02.h"

Frm02::Frm02(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm02::~Frm02() {
}

void Frm02::onInit() {

}

void Frm02::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- MButton : wMMButton1                 -----------------------------*/
	wMMButton1 = new STTButton(p_rt, this, "wMMButton1", 1);
	wMMButton1->setMainArg(0, 0, 93, 97, false);
	wMMButton1->setImg("1", "./images/arrow_34_93X97.gray.svg", "");
	wMMButton1->setImg("0", "./images/arrow_34_93X97.svg", "");
	wMMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- List : wMList1                       -----------------------------*/
	wMList1 = new STTList(p_rt, this, "wMList1");
	wMList1->setMainArg(0, 97, 1025, 501, false);
	wMList1->setFonts(0x0, 0xffffff, 16, "文泉驿微米黑", 0);
	wMList1->setMainStyle(10, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0);
	wMList1->setButtonStyleSelected(0, 0x1a2b1, 0xffffff, 0, 0);

	/*----------------------------- Switch : wMSwitch1                   -----------------------------*/
	wMSwitch1 = new STTSwitch(p_rt, this, "wMSwitch1");
	wMSwitch1->setMainArg(519, 12, 80, 49, false, false);
	wMSwitch1->setMainStyle(9999, 0x808080, 0xffffff, 0, 0, 0xc0c0c0, 0, 0x0);
	wMSwitch1->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x0, 0, 0xffffff);
	wMSwitch1->setKnobStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x0, 0, 0xffffff);
	wMSwitch1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMSwitch1_clk_cb);

	/*----------------------------- Label : wMLabel1                     -----------------------------*/
	wMLabel1 = new STTLabel(p_rt, this, "wMLabel1");
	wMLabel1->setMainArg(515, 61, 88, 25, "单曲循环", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel1->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel1->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(652, 13, 98, 71, false);
	wMButton1->setPressedArg("开始播放", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("开始播放", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton1_clk_cb);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(773, 13, 98, 71, false);
	wMButton2->setPressedArg("暂停", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("暂停", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton2_clk_cb);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(891, 13, 98, 71, false);
	wMButton3->setPressedArg("继续", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->setReleasedArg("继续", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton3_clk_cb);

	/*----------------------------- Video : wMVideo1                     -----------------------------*/
	wMVideo1 = new STTFFmpeg(p_rt, this, "wMVideo1");
	wMVideo1->channel = 0;
	wMVideo1->setRotate(0);
	wMVideo1->setMainArg(100, 10, 125, 77, "./images/");

	/*----------------------------- Slider : wMSlider1                   -----------------------------*/
	wMSlider1 = new STTSlider(p_rt, this, "wMSlider1");
	wMSlider1->setMainArg(257, 26, 208, 22, 70, 40, 100, false);
	wMSlider1->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider1->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider1->setKnobStyle(7, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider1->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm02::wMSlider1_vch_cb);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, this, "wMLabel2");
	wMLabel2->setMainArg(325, 61, 88, 25, "音量", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*called after widget created.*/

	return; //createParts END
}

#include "util/StringUtil.h"
#include "util/FileUtil.h"
#include "util/Util.h"

using namespace stt::util;

vector<string> getMp3() {
	/*wMButton1（刷新媒体列表）的点击事件*/
	vector<string> ret1;
	vector<string> paths;
#if __arm__
	paths.push_back("/customer");//搜索客户目录
	paths.push_back("/vendor");//搜索外设目录
#else
	paths.push_back("./images");//搜索图片目录
	paths.push_back("./res");//搜索资源目录
#endif
	vector<KV_Str_Str> ret;
	for(string path: paths) {
		FileUtil::getPathContent(path,ret);//搜索，注意文件太多可能会慢
	}
	for(KV_Str_Str kv: ret) {
		string filepath = kv.v + "/" + kv.k;
		if(StringUtil::endWith(filepath, ".mp3")) {
			ret1.push_back(filepath);
		}
	}
	return ret1;
}

void Frm02::onLoad() {
	vector<string> vec = getMp3();
	for(string s:vec) {
		wMList1->addOption(s, s);
	}
	wMVideo1->setPlayList(vec[0]);
	wMVideo1->setAutoRestart(true);
}

bool Frm02::onClosing() {
	return true;
}

void Frm02::onDispose() {

}

static void set_play_list(STTFFmpeg *video, bool single, string path) {
	vector<string> vec = getMp3();
	if(single && path!="") {
		video->setPlayList(path);
	} else {
		video->setPlayList(vec);
	}
}

void Frm02::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（开始播放）的点击事件*/
	set_play_list(wMVideo1, this->wMSwitch1->isOn(), wMList1->getSelectedText());
	wMVideo1->play();

	int vol = wMSlider1->getValue();//获取滑块值
	wMVideo1->setVolume(vol);//设置音量
}

void Frm02::wMSwitch1_clk_cb(uint16_t code, LvEvent e) {
	set_play_list(wMVideo1, this->wMSwitch1->isOn(), wMList1->getSelectedText());
}

void Frm02::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（暂停）的点击事件*/
	wMVideo1->pause();
}

void Frm02::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton3（继续）的点击事件*/
	wMVideo1->resume();
}

void Frm02::wMSlider1_vch_cb(uint16_t code, LvEvent e) {
	int vol = wMSlider1->getValue();//获取滑块值
	wMVideo1->setVolume(vol);//设置音量
}
